package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.PrivateMessage;
import com.czh.springboot04.service.PrivateMessageService;
import com.czh.springboot04.service.serviceimpl.PrivateMessageServiceImpl;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

@RestController
@RequestMapping("/PrivateMessageController")
@CrossOrigin
public class PrivateMessageController {

    @Autowired
    DataSource dataSource;
    @Autowired
    PrivateMessageService privateMessageService = new PrivateMessageServiceImpl();

    @PostMapping("/insertPrivateMessage")
    public R insertPrivateMessage(@RequestBody PrivateMessage privateMessage) {
        int i = privateMessageService.insertPrivateMessage(privateMessage);
        if (i != 0) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @PutMapping("/updatePrivateMessageById")
    public R updatePrivateMessageById(@RequestBody PrivateMessage privateMessage) {
        int i = privateMessageService.updatePrivateMessageById(privateMessage);
        if (i != 0) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @GetMapping("/selectPrivateMessageById")
    public R selectPrivateMessageById(Integer pmId) {
        PrivateMessage privateMessage = privateMessageService.selectPrivateMessageById(pmId);
        if (privateMessage != null) {
            return R.ok(privateMessage);
        } else {
            return R.fail("查询失败");
        }
    }

    @DeleteMapping("/deletePrivateMessageById/{pmId}")
    public R deletePrivateMessageById(@PathVariable("pmId") Integer pmId) {
        int i = privateMessageService.deletePrivateMessageById(pmId);
        if (i != 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @DeleteMapping("/deletePrivateMessageByList")
    public R deletePrivateMessageByList(@RequestBody List<Integer> pmIdList) {
        int i = privateMessageService.deletePrivateMessageByList(pmIdList);
        if (i != 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("/selectPrivateMessageAll")
    public R selectPrivateMessageAll(Integer pmId,
                                     Integer memberId,
                                     Integer consultId,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        PageInfo<PrivateMessage> privateMessagePageInfo = privateMessageService.selectPrivateMessageAll(pmId, memberId, consultId, pageNum, pageSize);
        if (privateMessagePageInfo != null) {
            return R.ok(privateMessagePageInfo);
        } else {
            return R.fail("查询失败");
        }
    }
}
